import { createStore } from 'vuex'

const setLocalCartList = (state) => {
  const { cartList } = state
  const cartListString = JSON.stringify(cartList)
  localStorage.cartList = cartListString
}

const getLocalCartList = () => {
  if (localStorage.cartList) {
    return JSON.parse(localStorage.cartList)
  }
  return {}
}

/*
  cartList数据结构：
  第一层是店铺信息 => 店铺名称和产品列表
  第二层是产品信息
  cartList: {
     "1": {
        "shopName": "沃尔玛",
        "productList": {
          "1": {
            "_id": "1",
            "name": "番茄 250g / 份",
            "imgUrl": "http://www.dell-lee.com/imgs/vue3/tomato.png",
            "sales": 10,
            "price": 33.6,
            "oldPrice": 39.6,
            "count": 0,
            "checked": true
          },
          "2": {
            "_id": "4",
            "name": "帝王蟹 250g / 份",
            "imgUrl": "http://www.dell-lee.com/imgs/vue3/crab.png",
            "sales": 10,
            "price": 199.9,
            "oldPrice": 299.9,
            "count": 1,
            "checked": false
          },
          ...
        }
      },
      "2": {
        "shopName": "沃尔玛",
        "productList": {
          "1": {
            "_id": "1",
            "name": "番茄 250g / 份",
            "imgUrl": "http://www.dell-lee.com/imgs/vue3/tomato.png",
            "sales": 10,
            "price": 33.6,
            "oldPrice": 39.6,
            "count": 2,
            "checked": true
          },
          "2": {
            "_id": "2",
            "name": "车厘子 500g / 份",
            "imgUrl": "http://www.dell-lee.com/imgs/vue3/cherry.png",
            "sales": 10,
            "price": 99.6,
            "oldPrice": 119.6,
            "count": 2,
            "checked": true
          },
          ...
        }
      }
    }
  }
*/

export default createStore({
  state: {
	  cartList: getLocalCartList()
  },
  mutations: {
  	changeToCart(state, payload) {
  		const { shopId, productId, product, num } = payload
  		const shopInfo = state.cartList[shopId] || { shopName: '', productList: {} }
  		let productInfo = shopInfo.productList[productId]
  		if (!productInfo) {
  			productInfo = product
  			productInfo.count = 0
  		}
  		productInfo.count += num
      if (num > 0) {
        productInfo.checked = true
      }
  		if (productInfo.count <= 0) {
  			productInfo.count = 0
  		}
  		if (productInfo.count >= 100) {
  			productInfo.count = 100
  		}
  		shopInfo.productList[productId] = productInfo
  		state.cartList[shopId] = shopInfo
      setLocalCartList(state)
  	},
    changeShopName(state, payload) {
      const { shopId, shopName } = payload
      const shopInfo = state.cartList[shopId] || { shopName: '', productList: {} }
      shopInfo.shopName = shopName
      state.cartList[shopId] = shopInfo
      setLocalCartList(state)
    },
    changeCartChecked(state, payload) {
      const { shopId, productId } = payload
      const product = state.cartList[shopId].productList[productId]
      product.checked = !product.checked
      setLocalCartList(state)
    },
    clearCartPoduct(state, payload) {
      state.cartList[payload.shopId].productList = {}
    },
    setCartChecked(state, payload) {
      const products = state.cartList[payload.shopId].productList
      if (products) {
        for (const key in products) {
          const product = products[key]
          product.checked = true
        }
      }
      setLocalCartList(state)
    },
    clearCart(state, payload) {
      const { shopId } = payload
      state.cartList[shopId].productList = {}
      setLocalCartList(state)
      state.cartList = getLocalCartList()
    }
  },
  actions: {},
  modules: {}
})
